<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>event-rule-addemsg</title>
    <%@ include file="../include/common.jsp"%>
    <%@ include file="../include/tree.jsp"%>
    <%@ include file="../include/form.jsp"%>
    <%@ include file="../include/jbox.jsp"%>
	<style type="text/css">
		.ztree {border: 1px solid #617775;background: #f0f6e4;width:220px;height:360px;overflow-y:scroll;overflow-x:auto;}
	</style>
	<link rel="stylesheet" media="all" type="text/css" href="${pageContext.request.contextPath}/style/default/tree/demo.css" />
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/default/easyui/easyui.css">
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery.easyui.min.js"></script>
	<script type="text/javascript">
	var propId = -1;
	function showResTree(){
			$.ajax({ 
				  type:"post", 
				  url:"${pageContext.request.contextPath}/resource/res-frame!resViewTree.action", 
				  data:"", 
				  datatype:"json",
				  success:function(data) { 
						var zNodes=eval(data);
						$.fn.zTree.init($("#treeDemo"), setting, zNodes);
							var obj = $("#resName");
							var offset = $("#resName").offset();
							$("#menuContent").css({left:offset.left + "px", top:offset.top + obj.outerHeight() + "px"}).slideDown("fast");
							$("body").bind("mousedown", onBodyDown);
				  } 
		     	})
	}
	var setting = {
			async: {
				enable: true,
				url:"${pageContext.request.contextPath}/resource/res-frame!resViewTree.action",
				dataType: "text",
				autoParam:["id"]
			},
			view: {
				dblClickExpand: false
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: relNodeClick,
				onCheck: onCheck
			}
		};
		function relNodeClick(e, treeId, treeNode) {
			if(treeNode.id.substring(0,1) == 2){
				$("#resId").val(treeNode.id.split('|')[1]);
				$("#resName").val(treeNode.name);
				hideMenu();
				initData();
			}else{
				$("#resId").val('');
				$("#resName").val('请选择资源');	
				initData();
			}
		}
		function onCheck(e, treeId, treeNode) {
			
		}
		function hideMenu() {
			$("#menuContent").fadeOut("fast");
			$("body").unbind("mousedown", onBodyDown);
		}
		function onBodyDown(event) {
			if (!(event.target.id == "menuBtn" || event.target.id == "resName" || $(event.target).parents("#menuContent").length>0)) {
				hideMenu();
			}
		}
		function initData(){
			$.ajax({
				   type: "post", 
				   url: "${pageContext.request.contextPath}/event/event-rule!propertyList.action?resId="+$("#resId").val(),
				   data: "", //传的参数
				   async:false,
				   dataType: "json",
				   success: function(data){ //结果
					   var obj= document.getElementById("propRuleSelect");
					   obj.length=0;
					   if(data != null){
						   for(var i = 0 ;i<data.length;i++){
								obj.options[i]=new Option(data[i].name,data[i].id);
					   	   }
					   }
				   }
				  
			});
			 $.ajax({
				   type: "post", 
				   url: "${pageContext.request.contextPath}/event/event-rule!attributeList.action?resId="+$("#resId").val(),
				   data: "", //传的参数
				   async:false,
				   dataType: "json",
				   success: function(data){ //结果
					   var obj= document.getElementById("attrRuleSelect");
					   obj.length=0;
					   if(data != null){
						   for(var i = 0 ;i<data.length;i++){
								obj.options[i]=new Option(data[i].name,data[i].id);
					   	   }
					   }
				   }
			});
		}
		$(function(){
			if('${defineEventType}' =='1'){
				$("#defaultTr").css({"display":"none"});
			}
			$("#ruleTextTd").attr("disabled",true);
			initData();
			if('${msgIsDefault}' == '1' || '${msgIsDefault}' == '0'){
				showRule('${msgIsDefault}');
				$("input[type=radio][value="+'${msgIsDefault}'+"]").attr("checked",'checked');
			}
		})
		function addRuleProp(){
			insertAtCaret(document.getElementById("ruleText"),$("#propRuleSelect").val());
			$.ajax({
				   type: "POST", 
				   url: "${pageContext.request.contextPath}/event/event-rule!havePropIdbyPropName.action?propRuleSelect="+encodeURI(encodeURI($("#propRuleSelect").val())), 
				   data: '', //传的参数
				   dataType: 'json',
				   success: function(data){ //结果
					   propId = data+'L';
				   }
			});
		}
		function addRuleAttr(){
			insertAtCaret(document.getElementById("ruleText"),$("#attrRuleSelect").val());
		}
		function addRuleFun(){
			if('lastPropValue' == $("#funRuleSelect").val()){
				if(propId == '-1'){
					getTopWin(window).getMessageBox().alert("上次指标为空","提示");
				}else{
					insertAtCaret(document.getElementById("ruleText"),"lastPropValue("+$("#resId").val()+","+propId+") && lastPropValue("+$("#resId").val()+","+propId+")!= null");
				}
			}
			else if('resname' == $("#funRuleSelect").val()){
				insertAtCaret(document.getElementById("ruleText"),"resname("+$("#resId").val()+":"+$("#resName").val()+")");
			}
			else if('ip' == $("#funRuleSelect").val()){
				insertAtCaret(document.getElementById("ruleText"),"ip("+$("#resId").val()+":"+$("#resName").val()+")");
			}
			else if('inc' == $("#funRuleSelect").val()){
				insertAtCaret(document.getElementById("ruleText"),"inc("+$("#resId").val()+":"+$("#resName").val()+",propId,time)");
			}
			else{
				insertAtCaret(document.getElementById("ruleText"),$("#funRuleSelect").val());
			}
		}
		function closewin(){
			  getTopWin(window).closejBox("addExpr-event-rule");
		}
		function add(){
			var radios = document.getElementsByName("radios");
			for(var i= 0 ; i< radios.length; i++){
				  if(radios[i].checked == true){
					  if(radios[i].value == 1){//默认
						  var pWin = getTopWin(window).currentWin["addMsg-event-rule"];
							 pWin.addMsgValue('${msg}','${msg1}','1');
							 getTopWin(window).closejBox("addMsg-event-rule");
					  }else{//自定义
						//校验
						  $.ajax({
							   type: "post", 
							   url: "${pageContext.request.contextPath}/event/event-rule!validateMsg.action", //请求的action
							   data: "msg="+encodeURI(encodeURI($("#ruleText").val())),
							   dataType: "json",
							   success: function(data){ //结果
								   	  if(data == true){
								   		$.ajax({
											   type: "post", 
											   url: "${pageContext.request.contextPath}/event/event-rule!addMsgValue.action?resId="+$("#resId").val()+"&eventTypeId="+$("#eventTypeId").val(), //请求的action
											   data: "text="+encodeURI(encodeURI($("#ruleText").val())),
											   dataType: "json",
											   success: function(data){ //结果
												   var pWin = getTopWin(window).currentWin["addMsg-event-rule"];
													 pWin.addMsgValue($("#ruleText").val().replace( /(^\s*)|(\s*$)/g, ''),data.msg,'0','${defineEventType}');
													 getTopWin(window).closejBox("addMsg-event-rule");
											   }
										});
								   	  }else{
								   		getTopWin(window).getMessageBox().alert("消息表达式格式错误","提示");
								   	  }
							   }
						});
					  }
				  }
			}
		}
		function view(){
			var msg;
			var radios = document.getElementsByName("radios");
			for(var i= 0 ; i< radios.length; i++){
				  if(radios[i].checked == true){
					  if(radios[i].value == 1){//默认
						  msg = '${msg}';
					  }else{//自定义
						  msg = $("#ruleText").val();
					  }
				  }
			}
			var url ="iframe:event/event-rule!viewMsg.action?msg="+encodeURI(encodeURI(msg))+"&resId="+'${resId}';
			var opt = { title: "消息预览",width:400,height:100,buttons: {}};
			getTopWin(window).openjBox(url,opt,'addExpr-event-rule',window);
		}
		function showRule(flag){
			if(flag == 1){
				  $("#exprTd").attr("disabled",false);
				  $("#resName").attr("disabled",true); 
			      $("#propRuleSelect").attr("disabled",true);
			      $("#attrRuleSelect").attr("disabled",true);
			      $("#funRuleSelect").attr("disabled",true);
			      $("#ruleText").attr("disabled",true);
			      $("#ruleTextTd").attr("disabled",true);
			      $("#viewId").css({"display":"none"});
			}else{
				  $("#exprTd").attr("disabled",true);
			      $("#resName").attr("disabled",false); 
			      $("#propRuleSelect").attr("disabled",false);
			      $("#attrRuleSelect").attr("disabled",false);
			      $("#funRuleSelect").attr("disabled",false);
			      $("#ruleText").attr("disabled",false);
			      $("#ruleTextTd").attr("disabled",false);
			      $("#viewId").css({"display":""});
			}
		}
		function storeRuleCaret (textEl){
		      if (textEl.createTextRange){
			      textEl.caretPos = document.selection.createRange().duplicate();
		      }
		 }
		 function insertAtCaret (textEl, text) {
		      if (textEl.createTextRange && textEl.caretPos) {
		        var caretPos = textEl.caretPos;
		        caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;
		      }else{
		    	  textEl.value = text;
		      }
		 }
	</script>
  </head>
  
  <body>
  	<div id="menuContent" style="display:none; position: absolute;z-index:100;">
		<ul id="treeDemo" class="ztree" style="margin-top:0; width:200px; height: 200px;"></ul>
	</div>
    <form action="">
    	<table class="table_eidt">			
    					<tr height="40px" id="defaultTr">
    						<td style="width: 80px;"  class="td_left">
    							<input type="radio" name="radios" id="radios" value="1" checked="checked" onclick="showRule(1);">
    							<label>默认</label>
    						</td>
    						<td id="exprTd"  class="td_right">
    							${msg}
    						</td>
    					</tr>
    					<tr>
    						<td  class="td_left">
    							<input type="radio" name="radios" id="radios" value="0" onclick="showRule(0);">
    							<label>自定义</label>
    						</td>
    						<td id="ruleTextTd"  class="td_right">
    							<table>
			    					<tr>
			    						<td>
						    				<label>选择资源：</label>
						    				<input id="resName" type="text" name="resName" readonly value='${resName}' onclick="showResTree(); return false;" disabled="disabled" class="input_text tree_res"/>
						    				<input id="resId" name="resId" type="hidden" value=${resId}>
						    			</td>
			    					</tr>
			    					<tr>
			    						<td>
			    							<select name="propRuleSelect" id="propRuleSelect" onchange="addRuleProp();" style="width: 150px;" disabled="disabled">
									        </select>
						    				<select name="attrRuleSelect" id="attrRuleSelect" onchange="addRuleAttr();" style="width: 150px;" disabled="disabled">
									        </select>
						    				<select name="funRuleSelect" id="funRuleSelect" onchange="addRuleFun();" style="width: 150px;" disabled="disabled">
						                        <option value="">选择函数</option>
						                        <option value="resname">资源名称</option>
						                        <option value="ip">资源IP</option>
						                        <option value="round(d)">四舍五入</option>
						                        <option value="round(d,2)">四舍五入(值,2)</option>
						                        <option value="inc">增长率</option>
						                        <option value="lastPropValue">上次指标值</option>
						                    </select>
			    						</td>
			    					</tr>
			    					<tr>
			    						<td colspan="3">
						                    <textarea name="ruleText" style="width: 455px;height: 200px" id="ruleText" disabled="disabled" onselect="storeRuleCaret(this);" onclick="storeRuleCaret(this);" onkeyup="storeRuleCaret(this);">${msg2}</textarea>
						                 </td>
			    					</tr>
			    				</table>
    						</td>
    					</tr>
    				</table>
    	<div align="center" style="margin-top:5px;">
    			<input type="button" id="viewId"  value="消息预览" class="btn-4" onclick="view();" style="display: none;">
		    	<input type="button" value="提交" class="btn-4" onclick="add();">
		    	<input type="button" value="返回" onclick="closewin();" class="btn-4">
    		</div>
    </form>
  </body>
</html>
